package com.nian.user.center.config;

/**
 * 用途：
 * 开发：zhangmj
 * 日期：2020/4/5 0005 21:02
 */

import com.nian.user.center.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;

//@Configuration
//@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private UserServiceImpl userService;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient(RoleType.ROLE_ADMIN)
                .secret(passwordEncoder.encode("123456"))//配置 client_secret
                .accessTokenValiditySeconds(3600) // token的有效期
                .refreshTokenValiditySeconds(864000)//刷新token的有效期
                .redirectUris("https://www.baidu.com")//redirect_uri，用于授权成功后跳转
                .scopes("all")//申请的权限范围
                .authorizedGrantTypes("authorization_code","password");//grant_type，表示授权类型
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager).userDetailsService(userService);
    }
}
